12. Pull database file from device

Pull Database File From Device

Instructions to pull database file from device

**Note: ** This method works on emulators and rooted physical devices.

1. In the CatalogActivity of our project, comment out where we call displayDatabaseInfo() method and instead execute just the first statement where I create a new instance of PetDbHelper:

PetDbHelper mDbHelper = new PetDbHelper(this);

2. On the device, go to Settings > Apps > Find the app in question > Clear Cache. This erases any pre-existing sqlite database for the app.

Clear Cache from Settings for the app

Clear Cache from Settings for the app

3. If I go back to our app, nothing appears in the Catalog Activity now because I’m not calling displayDatabaseInfo(). At this point, no database has been created because we did not call getWriteableDatabase() or getReadableDatabase().

To confirm no database has been created on the device yet -

  • Open the Android Device Monitor

  • Click the green Android icon to open the File Explorer to show the device's file system

  • Go to data/data/ (e.g. com.example.android.pets) to see the data associated with that application

File Explorer in Android Device Monitor

File Explorer in Android Device Monitor

Go to the data associated with the package

Go to the data associated with the package

4. To actually create the database (if no database exists) I need to add this statement -
SQLiteDatabase db = mDbHelper.getReadableDatabase();

Once it's added, rerun the app and see that the database called shelter.db was created in the File Explorer.

5. To see the contents of shelter.db, I can download it through DDMS by pressing the pull a file from device icon. I’ll save it on the desktop.

6. Then going into SQLite, I can use .open to open up the database. Use .tables to see there are two tables, android_metadata, which is a table automatically created by the Android system, and pets, which was created in the onCreate() method of PetDbHelper.

Use PRAGMA TABLE_INFO(pets) to see the columns in the database.

Similarly .schema shows me the sqlite statement I used in my code to make the table

7. If your table does not look like this, then something is wrong with your creation statement here. You can erase the database by uninstalling the app. Then you can fix the code and reinstall the app. When the databases match and it's working correctly, you can move on to the next step.

Also, if you want to clear out the databases at any time, or to run the onCreate() code again, they can manually clear out the app. From Settings > Select Interest App > hit “Force Stop” and “Clear Data”. Note that “Clear Data” will also “Clear Cache”.